package com.gs.wzweather.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gs.wzweather.bo.LatestWea;
import com.gs.wzweather.bo.Response;
import com.gs.wzweather.dao.SurHorDao;
import com.gs.wzweather.entity.*;
import com.gs.wzweather.service.SurHorService;
import com.gs.wzweather.utils.Constant;
import com.gs.wzweather.utils.DateUtil;
import com.gs.wzweather.utils.HttpUtils;
import com.gs.wzweather.utils.ParseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.FutureTask;

/*
@Component
@Repository
@Service
@Controller
*/

@Service
public class SurHorServiceImpl implements SurHorService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SurHorDao surHorDao;
    Gson gson = new Gson();

    @Override
    public Response queryWea(String pastH, String element, String type) {
        Response response = new Response();
        List<Site> siteList = null;
        try {
            String endTime = DateUtil.getTime();
            String beginTime = DateUtil.subTime(endTime, pastH);
            logger.info("Time->{}", beginTime + " " + endTime);
            switch (type == null ? Constant.HorType.NORMAL : type) {
                case Constant.HorType.MAX:
                    siteList = surHorDao.queryMax(beginTime, endTime, element);
                    break;
                case Constant.HorType.Min:
                    siteList = surHorDao.queryMin(beginTime, endTime, element);
                    break;
                case Constant.HorType.AVG:
                    siteList = surHorDao.queryAvg(beginTime, endTime, element);
                    break;
                default:
            }
            //检查空值站点是否超过10个
            String staIds = ParseUtil.checkIsNull(siteList);
            if (staIds == null) {//不超过10个 正常返回
                response.setReturnCode(Constant.Response.SUCCESS_CODE);
                response.setSiteList(ParseUtil.formatSites(siteList));
                return response;
            }
            //空值超过10个 请求最新数据来代替空数据
            FutureTask<LatestWea> latestWea = new FutureTask(() -> {
                LatestWea lw = getLatestWea(staIds, element);
                return lw;
            });
            new Thread(latestWea).start();
            LatestWea latestWeaResult = latestWea.get();
            if (latestWeaResult.getReturnCode() == 0) {
                ParseUtil.parseLatestWea(siteList, latestWeaResult);

            }
            response.setReturnCode(Constant.Response.SUCCESS_CODE);
            response.setSiteList(ParseUtil.formatSites(siteList));
            return response;
        } catch (Exception e) {
            logger.info("queryWea->{}", e.getMessage());
            return response;
        }
    }


    public LatestWea getLatestWea(String staId, String element) {
        String url = Constant.Url.LATEST_WEA + Constant.Url.PARAM_STAIDS + staId + Constant.Url.PARAM_ELEMENT + Constant.Url.ELEMENT_STATION_ID + element;
        String result = HttpUtils.request(url);
        LatestWea latestWea = gson.fromJson(result, new TypeToken<LatestWea>() {
        }.getType());
        return latestWea;
    }
}
